package com.kanche.bigbaby.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.adapter.SmListViewAdapter;
import com.kanche.bigbaby.bean.LoginUser;
import com.kanche.bigbaby.collection.dialog.YesOrNoDialog;
import com.kanche.bigbaby.support.base.BasePage;
import com.kanche.bigbaby.support.ddlv.ToastUtils;
import com.kanche.bigbaby.support.smlistview.SwipeMenu;
import com.kanche.bigbaby.support.smlistview.SwipeMenuCreator;
import com.kanche.bigbaby.support.smlistview.SwipeMenuItem;
import com.kanche.bigbaby.support.smlistview.SwipeMenuListView;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.HeaderTransformer;
import uk.co.senab.actionbarpulltorefresh.library.Options;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.AbsListViewDelegate;

/**
 * Created by Chenshao_Young on 15/5/19.
 */
public class SmListViewPage extends BasePage implements OnRefreshListener,
        SwipeMenuListView.OnLoadMoreListener{

    String pageTitle;
//    SwipeRefreshLayout srl_page;
    PullToRefreshLayout ptr_page;
    SwipeMenuListView smlv_page;
    SmListViewAdapter adapter;
    View view;
    //一共有10中情况
    int from = 0b0000;

    public SmListViewPage(Fragment fragment,String pageTitle) {
        super(fragment);
        this.pageTitle = pageTitle;
    }
    public SmListViewPage(Context context,String pageTitle) {
        super(context);
        this.pageTitle = pageTitle;
    }
    public SmListViewPage(Context context,String pageTitle,int from) {
        super(context);
        this.pageTitle = pageTitle;
        this.from = from;
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.page_smlistview, null);
        smlv_page = justFindIt(view, R.id.smlv_page);
        ptr_page = justFindIt(view,R.id.ptr_page);

        //ptr

        try {
            if(null == activity){
                throw new Exception("This page need a Activity context");
            }
        } catch (Exception e){
            //怎么kill 掉
        }
        ActionBarPullToRefresh.from(activity)
                .options(Options.create()
                        .scrollDistance(.50f)
//                        .headerLayout(R.layout.header_ptractionbar2)
//                        .headerTransformer(new CustomisedHeaderTransformer())
                        .build())
                .allChildrenArePullable()
                .listener(this)
                        // Here we'll set a custom ViewDelegate
                .useViewDelegate(GridView.class, new AbsListViewDelegate())
                .setup(ptr_page);

//        ptr_page.getHeaderView().setBackgroundColor(context.getResources().getColor(R.color.tb_colorPrimaryDark));

//        ptr_page.getHeaderView().findViewById(R.id.ptr_content).setBackgroundColor(context.getResources().getColor(R.color.tb_colorPrimaryDark));


        //lv
        smlv_page.setmOnLoadMoreListener(this);
        List<LoginUser> list = new ArrayList<>();
        for(int i=0;i<20;i++){
            list.add(new LoginUser());
        }
        adapter = new SmListViewAdapter(context,list);
        smlv_page.setAdapter(adapter);
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {

                switch (menu.getViewType()){
                    case 0:{

                        switch (from){
                            //上游库存第一个
                            case 0b0000:{

                                SwipeMenuItem uploadItem = new SwipeMenuItem(context);
                                uploadItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
                                uploadItem.setWidth(90);
                                uploadItem.setTitle("上架");
                                uploadItem.setTitleSize(18);
                                uploadItem.setTitleColor(Color.WHITE);
                                menu.addMenuItem(uploadItem);

                                SwipeMenuItem editItem = new SwipeMenuItem(context);
                                editItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                                editItem.setWidth(90);
                                editItem.setTitle("售出");
                                editItem.setTitleSize(18);
                                editItem.setTitleColor(Color.BLACK);
//                editItem.setIcon(R.drawable.ic_delete);
                                menu.addMenuItem(editItem);
                                break;
                            }
                        }
                        break;
                    }
                    case 1:{

                        switch (from){
                            //上游库存第一个
                            case 0b0000:{

                                SwipeMenuItem uploadItem = new SwipeMenuItem(context);
                                uploadItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
                                uploadItem.setWidth(90);
                                uploadItem.setTitle("下架架");
                                uploadItem.setTitleSize(18);
                                uploadItem.setTitleColor(Color.WHITE);
                                menu.addMenuItem(uploadItem);

                                SwipeMenuItem editItem = new SwipeMenuItem(context);
                                editItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                                editItem.setWidth(90);
                                editItem.setTitle("售出");
                                editItem.setTitleSize(18);
                                editItem.setTitleColor(Color.BLACK);
//                editItem.setIcon(R.drawable.ic_delete);
                                menu.addMenuItem(editItem);
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        };
        smlv_page.setMenuCreator(creator);
        smlv_page.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
//                ApplicationInfo item = mAppList.get(position);
                switch (index) {
                    //first
                    case 0:

                        switch (from) {
                            //上游库存第一个tab
                            case 0b0000: {
                                if (menu.getViewType() == 0) {

                                YesOrNoDialog dialog = new YesOrNoDialog();
                                Bundle arg = new Bundle();
                                arg.putString(YesOrNoDialog.MESSAGE, "奔驰拉风尊贵自动吊炸天款\n售价55");
                                arg.putString(YesOrNoDialog.STRPositive, "确认上架");
                                arg.putString(YesOrNoDialog.TITLE, "上架");
                                dialog.setArguments(arg);
                                dialog.setOnClick(new YesOrNoDialog.OnClick() {
                                    @Override
                                    public void onPositive() {

                                        ToastUtils.show(context, "yes");
                                    }

                                    @Override
                                    public void onNegative() {
                                        ToastUtils.show(context, "nonono");

                                    }
                                });
                                fragmentActivity.getSupportFragmentManager().beginTransaction().add(dialog, "YNDialog").commit();

                                break;
                            }
                        }

                            }
                        break;
                    case 1:

                        switch (from) {
                            //上游库存第一个
                            case 0b0000: {

                                ToastUtils.show(context,"售出");

                                break;
                            }

                        }
                        break;
                }

                return false;
            }
        });
        
        return view;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void loadMore() {
        
    }

    @Override
    public void onRefreshStarted(View view) {

    }


    /**
     * Here's a customised header transformer which displays the scroll progress as text.
     */
    static class CustomisedHeaderTransformer extends HeaderTransformer {

        private View mHeaderView;
        private TextView mMainTextView;
        private TextView mProgressTextView;

        @Override
        public void onViewCreated(Activity activity, View headerView) {
            mHeaderView = headerView;
            mMainTextView = (TextView) headerView.findViewById(R.id.ptr_text);
            mProgressTextView = (TextView) headerView.findViewById(R.id.ptr_text_secondary);
        }

        @Override
        public void onReset() {
            mMainTextView.setVisibility(View.VISIBLE);
            mMainTextView.setText(R.string.pull_to_refresh_pull_label);

            mProgressTextView.setVisibility(View.GONE);
            mProgressTextView.setText("");
        }

        @Override
        public void onPulled(float percentagePulled) {
            mProgressTextView.setVisibility(View.VISIBLE);
            mProgressTextView.setText(Math.round(100f * percentagePulled) + "%");
        }

        @Override
        public void onRefreshStarted() {
            mMainTextView.setText(R.string.pull_to_refresh_refreshing_label);
            mProgressTextView.setVisibility(View.GONE);
        }

        @Override
        public void onReleaseToRefresh() {
            mMainTextView.setText(R.string.pull_to_refresh_release_label);
        }

        @Override
        public void onRefreshMinimized() {
            // In this header transformer, we will ignore this call
        }

        @Override
        public boolean showHeaderView() {
            final boolean changeVis = mHeaderView.getVisibility() != View.VISIBLE;
            if (changeVis) {
                mHeaderView.setVisibility(View.VISIBLE);
            }
            return changeVis;
        }

        @Override
        public boolean hideHeaderView() {
            final boolean changeVis = mHeaderView.getVisibility() == View.VISIBLE;
            if (changeVis) {
                mHeaderView.setVisibility(View.GONE);
            }
            return changeVis;
        }
    }
}
